package com.xy.annotaion.handler.kotlin.graph

import org.jfree.pdf.PDFDocument
import org.jfree.pdf.PDFHints
import java.awt.BorderLayout
import java.awt.Rectangle
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.io.File
import javax.swing.*


/**
 * This demo shows how to export a Swing UI to PDF.
 */
class SwingUIToPDFDemo(title: String?) : JFrame(title), ActionListener {
    private fun createContent(): JComponent {
        val content = JPanel(BorderLayout())
        val tabs = JTabbedPane()
        tabs.add("Tab 1", JButton("First Tab"))
        tabs.add("Tab 2", JButton("Second Tab"))
        val button = JButton("Save to PDF")
        button.addActionListener(this)
        content.add(tabs)
        content.add(button, BorderLayout.SOUTH)
        return content
    }

    override fun actionPerformed(e: ActionEvent) {
        val c = contentPane.getComponent(0) as JComponent
        val pdfDoc = PDFDocument()
        pdfDoc.title = "SwingUIToPDFDemo.java"
        pdfDoc.author = "Object Refinery Limited"
        val page = pdfDoc.createPage(Rectangle(c.width, c.height))
        val g2 = page.graphics2D
        g2.setRenderingHint(
            PDFHints.KEY_DRAW_STRING_TYPE,
            PDFHints.VALUE_DRAW_STRING_TYPE_VECTOR
        )
        c.paint(g2)
        val f = File("SwingUIToPDFDemo.pdf")
        pdfDoc.writeToFile(f)
    }

    init {
        add(createContent())
    }
}

fun main() {
    try {
        for (info in UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus" == info.name) {
                UIManager.setLookAndFeel(info.className)
                break
            }
        }
    } catch (e: Exception) {
        // If Nimbus is not available, you can set the GUI to another look and feel.
        e.printStackTrace()
    }
    val app = SwingUIToPDFDemo("SwingUIToPDFDemo.java")
    app.pack()
    app.isVisible = true
}